Required libraries

Expolring and cleaning the data set

element_analysis function

Element analysis results

# Example of how to use the function with your data
results <- analyze_elements(long_dat)

# Example of how to access results for a specific element
results[["Ph"]]$SummaryStats
## # A tibble: 2 × 4
##   WormPresence  Mean    SD Median
##          <dbl> <dbl> <dbl>  <dbl>
## 1            0  7.17 0.209   7.21
## 2            1  7.18 0.224   7.16
results[["Ph"]]$ANOVA
## # A tibble: 2 × 6
##   term            df   sumsq  meansq statistic p.value
##   <chr>        <dbl>   <dbl>   <dbl>     <dbl>   <dbl>
## 1 WormPresence     1 0.00125 0.00125    0.0267   0.871
## 2 Residuals       52 2.43    0.0468    NA       NA
results[["SS"]]$SummaryStats
## # A tibble: 2 × 4
##   WormPresence  Mean     SD Median
##          <dbl> <dbl>  <dbl>  <dbl>
## 1            0 0.210 0.107    0.19
## 2            1 0.200 0.0303   0.19
results[["SS"]]$ANOVA
## # A tibble: 2 × 6
##   term            df   sumsq  meansq statistic p.value
##   <chr>        <dbl>   <dbl>   <dbl>     <dbl>   <dbl>
## 1 WormPresence     1 0.00135 0.00135     0.219   0.642
## 2 Residuals       52 0.320   0.00616    NA      NA
results[["NitN"]]$SummaryStats
## # A tibble: 2 × 4
##   WormPresence   Mean     SD Median
##          <dbl>  <dbl>  <dbl>  <dbl>
## 1            0 0.0178 0.0169   0.01
## 2            1 0.0230 0.0441   0.01
results[["NitN"]]$ANOVA
## # A tibble: 2 × 6
##   term            df    sumsq   meansq statistic p.value
##   <chr>        <dbl>    <dbl>    <dbl>     <dbl>   <dbl>
## 1 WormPresence     1 0.000363 0.000363     0.325   0.571
## 2 Residuals       52 0.0580   0.00112     NA      NA
results[["AmoN"]]$SummaryStats
## # A tibble: 2 × 4
##   WormPresence  Mean    SD Median
##          <dbl> <dbl> <dbl>  <dbl>
## 1            0 0.309 0.257   0.24
## 2            1 0.275 0.266   0.17
results[["AmoN"]]$ANOVA
## # A tibble: 2 × 6
##   term            df  sumsq meansq statistic p.value
##   <chr>        <dbl>  <dbl>  <dbl>     <dbl>   <dbl>
## 1 WormPresence     1 0.0157 0.0157     0.229   0.634
## 2 Residuals       52 3.56   0.0684    NA      NA
results[["CA"]]$SummaryStats
## # A tibble: 2 × 4
##   WormPresence  Mean    SD Median
##          <dbl> <dbl> <dbl>  <dbl>
## 1            0  14.7  6.49   13.4
## 2            1  18.8  5.63   20
results[["CA"]]$ANOVA
## # A tibble: 2 × 6
##   term            df sumsq meansq statistic p.value
##   <chr>        <dbl> <dbl>  <dbl>     <dbl>   <dbl>
## 1 WormPresence     1  219.  219.       5.93  0.0184
## 2 Residuals       52 1920.   36.9     NA    NA
results[["Po"]]$SummaryStats
## # A tibble: 2 × 4
##   WormPresence  Mean    SD Median
##          <dbl> <dbl> <dbl>  <dbl>
## 1            0  13.1  6.17   12.3
## 2            1  10.8  4.36   10.0
results[["Po"]]$ANOVA
## # A tibble: 2 × 6
##   term            df  sumsq meansq statistic p.value
##   <chr>        <dbl>  <dbl>  <dbl>     <dbl>   <dbl>
## 1 WormPresence     1   72.0   72.0      2.52   0.119
## 2 Residuals       52 1487.    28.6     NA     NA
results[["Mag"]]$SummaryStats
## # A tibble: 2 × 4
##   WormPresence  Mean    SD Median
##          <dbl> <dbl> <dbl>  <dbl>
## 1            0  2.41 0.801   2.24
## 2            1  2.87 0.705   3
results[["Mag"]]$ANOVA
## # A tibble: 2 × 6
##   term            df sumsq meansq statistic p.value
##   <chr>        <dbl> <dbl>  <dbl>     <dbl>   <dbl>
## 1 WormPresence     1  2.86  2.86       5.02  0.0293
## 2 Residuals       52 29.6   0.570     NA    NA
results[["Pho"]]$SummaryStats
## # A tibble: 2 × 4
##   WormPresence  Mean    SD Median
##          <dbl> <dbl> <dbl>  <dbl>
## 1            0  2.57  2.82  1.72 
## 2            1  1.92  2.83  0.906
results[["Pho"]]$ANOVA
## # A tibble: 2 × 6
##   term            df  sumsq meansq statistic p.value
##   <chr>        <dbl>  <dbl>  <dbl>     <dbl>   <dbl>
## 1 WormPresence     1   5.70   5.70     0.715   0.402
## 2 Residuals       52 415.     7.97    NA      NA
results[["AL"]]$SummaryStats
## # A tibble: 2 × 4
##   WormPresence  Mean     SD Median
##          <dbl> <dbl>  <dbl>  <dbl>
## 1            0 0.114 0.0334  0.109
## 2            1 0.113 0.0334  0.101
results[["AL"]]$ANOVA
## # A tibble: 2 × 6
##   term            df     sumsq    meansq statistic p.value
##   <chr>        <dbl>     <dbl>     <dbl>     <dbl>   <dbl>
## 1 WormPresence     1 0.0000190 0.0000190    0.0170   0.897
## 2 Residuals       52 0.0579    0.00111     NA       NA
results[["BO"]]$SummaryStats
## # A tibble: 2 × 4
##   WormPresence   Mean     SD Median
##          <dbl>  <dbl>  <dbl>  <dbl>
## 1            0 0.0406 0.0193  0.034
## 2            1 0.0369 0.0180  0.031
results[["BO"]]$ANOVA
## # A tibble: 2 × 6
##   term            df    sumsq   meansq statistic p.value
##   <chr>        <dbl>    <dbl>    <dbl>     <dbl>   <dbl>
## 1 WormPresence     1 0.000189 0.000189     0.543   0.465
## 2 Residuals       52 0.0181   0.000348    NA      NA
results[["CO"]]$SummaryStats
## # A tibble: 2 × 4
##   WormPresence    Mean      SD Median
##          <dbl>   <dbl>   <dbl>  <dbl>
## 1            0 0.00565 0.00394  0.005
## 2            1 0.00561 0.00307  0.006
results[["CO"]]$ANOVA
## # A tibble: 2 × 6
##   term            df        sumsq       meansq statistic p.value
##   <chr>        <dbl>        <dbl>        <dbl>     <dbl>   <dbl>
## 1 WormPresence     1 0.0000000150 0.0000000150   0.00120   0.972
## 2 Residuals       52 0.000648     0.0000125     NA        NA
results[["IR"]]$SummaryStats
## # A tibble: 2 × 4
##   WormPresence   Mean     SD Median
##          <dbl>  <dbl>  <dbl>  <dbl>
## 1            0 0.0347 0.0113  0.033
## 2            1 0.0298 0.0100  0.029
results[["IR"]]$ANOVA
## # A tibble: 2 × 6
##   term            df    sumsq   meansq statistic p.value
##   <chr>        <dbl>    <dbl>    <dbl>     <dbl>   <dbl>
## 1 WormPresence     1 0.000328 0.000328      2.88  0.0958
## 2 Residuals       52 0.00592  0.000114     NA    NA
results[["Man"]]$SummaryStats
## # A tibble: 2 × 4
##   WormPresence     Mean       SD Median
##          <dbl>    <dbl>    <dbl>  <dbl>
## 1            0 0.00122  0.00318       0
## 2            1 0.000222 0.000424      0
results[["Man"]]$ANOVA
## # A tibble: 2 × 6
##   term            df     sumsq     meansq statistic p.value
##   <chr>        <dbl>     <dbl>      <dbl>     <dbl>   <dbl>
## 1 WormPresence     1 0.0000135 0.0000135       2.63   0.111
## 2 Residuals       52 0.000267  0.00000514     NA     NA
results[["MO"]]$SummaryStats
## # A tibble: 2 × 4
##   WormPresence    Mean      SD Median
##          <dbl>   <dbl>   <dbl>  <dbl>
## 1            0 0.0127  0.0282  0.0049
## 2            1 0.00525 0.00490 0.005
results[["MO"]]$ANOVA
## # A tibble: 2 × 6
##   term            df    sumsq   meansq statistic p.value
##   <chr>        <dbl>    <dbl>    <dbl>     <dbl>   <dbl>
## 1 WormPresence     1 0.000755 0.000755      1.85   0.180
## 2 Residuals       52 0.0213   0.000409     NA     NA
results[["SO"]]$SummaryStats
## # A tibble: 2 × 4
##   WormPresence  Mean    SD Median
##          <dbl> <dbl> <dbl>  <dbl>
## 1            0  20.5  3.17   20.3
## 2            1  21.9  3.70   21
results[["SO"]]$ANOVA
## # A tibble: 2 × 6
##   term            df sumsq meansq statistic p.value
##   <chr>        <dbl> <dbl>  <dbl>     <dbl>   <dbl>
## 1 WormPresence     1  25.7   25.7      2.16   0.147
## 2 Residuals       52 618.    11.9     NA     NA
results[["SU"]]$SummaryStats
## # A tibble: 2 × 4
##   WormPresence  Mean    SD Median
##          <dbl> <dbl> <dbl>  <dbl>
## 1            0  6.16  2.83   5.51
## 2            1  5.17  2.57   4.92
results[["SU"]]$ANOVA
## # A tibble: 2 × 6
##   term            df sumsq meansq statistic p.value
##   <chr>        <dbl> <dbl>  <dbl>     <dbl>   <dbl>
## 1 WormPresence     1  13.2  13.2       1.81   0.184
## 2 Residuals       52 380.    7.30     NA     NA
results[["Zi"]]$SummaryStats
## # A tibble: 2 × 4
##   WormPresence   Mean     SD Median
##          <dbl>  <dbl>  <dbl>  <dbl>
## 1            0 0.0620 0.0463  0.045
## 2            1 0.056  0.0274  0.05
results[["Zi"]]$ANOVA
## # A tibble: 2 × 6
##   term            df    sumsq   meansq statistic p.value
##   <chr>        <dbl>    <dbl>    <dbl>     <dbl>   <dbl>
## 1 WormPresence     1 0.000492 0.000492     0.340   0.562
## 2 Residuals       52 0.0752   0.00145     NA      NA
# ------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------

analyze_elements_detailed <- function(data) {
  results <- list()  # Create an empty list to store results
  
  # Loop over each element
  unique_elements <- unique(data$element)
  for (elem in unique_elements) {
    # Filter data for the specific element
    elem_data <- data %>% filter(element == elem)
    
    # Calculate summary statistics
    summary_stats <- elem_data %>% 
      group_by(Plant, WormPresence) %>% 
      summarise(
        Mean = mean(amount, na.rm = TRUE),
        SD = sd(amount, na.rm = TRUE),
        Median = median(amount, na.rm = TRUE),
        .groups = 'drop'
      )
    
    # Perform ANOVA to compare groups including interaction terms
    if (n_distinct(elem_data$WormPresence) > 1 && n_distinct(elem_data$Plant) > 1) {
      aov_results <- aov(amount ~ WormPresence * Plant, data = elem_data)
      tidy_aov <- tidy(aov_results)
    } else {
      tidy_aov <- tibble(term = NA, estimate = NA, statistic = NA, p.value = NA)
    }
    
    # Store results in the list
    results[[elem]] <- list(
      SummaryStats = summary_stats,
      ANOVA = tidy_aov
    )
  }
  
  return(results)
}

# Example of how to use the function with your data
results_detailed <- analyze_elements_detailed(long_dat)

# Example of how to access results for a specific element
if (!is.null(results_detailed[["Ph"]])) {
  print(results_detailed[["Ph"]]$SummaryStats)
  print(results_detailed[["Ph"]]$ANOVA)
}
## # A tibble: 14 × 5
##    Plant     WormPresence  Mean     SD Median
##    <chr>            <dbl> <dbl>  <dbl>  <dbl>
##  1 Chives               0  7.24 0.0971   7.22
##  2 Chives               1  7.09 0.0651   7.09
##  3 Cilantro             0  7.25 0.115    7.32
##  4 Cilantro             1  7.50 0.0306   7.49
##  5 Larkspur             0  7.23 0.0871   7.24
##  6 Larkspur             1  7.21 0.193    7.32
##  7 Mulch                0  6.86 0.279    6.75
##  8 Mulch                1  6.88 0.213    6.86
##  9 Onion                0  7.23 0.125    7.29
## 10 Onion                1  7.25 0.154    7.27
## 11 Scallion             0  7.28 0.153    7.2 
## 12 Scallion             1  7.17 0.0603   7.16
## 13 Whitelace            0  7.23 0.0833   7.26
## 14 Whitelace            1  7.31 0.0737   7.28
## # A tibble: 4 × 6
##   term                  df   sumsq  meansq statistic     p.value
##   <chr>              <dbl>   <dbl>   <dbl>     <dbl>       <dbl>
## 1 WormPresence           1 0.00125 0.00125    0.0519  0.821     
## 2 Plant                  6 1.32    0.219      9.09    0.00000280
## 3 WormPresence:Plant     6 0.154   0.0257     1.06    0.400     
## 4 Residuals             40 0.965   0.0241    NA      NA
if (!is.null(results_detailed[["SS"]])) {
  print(results_detailed[["SS"]]$SummaryStats)
  print(results_detailed[["SS"]]$ANOVA)
}
## # A tibble: 14 × 5
##    Plant     WormPresence  Mean     SD Median
##    <chr>            <dbl> <dbl>  <dbl>  <dbl>
##  1 Chives               0 0.22  0.0361   0.23
##  2 Chives               1 0.197 0.0208   0.19
##  3 Cilantro             0 0.19  0.0100   0.19
##  4 Cilantro             1 0.203 0.0321   0.19
##  5 Larkspur             0 0.29  0.229    0.19
##  6 Larkspur             1 0.202 0.0130   0.2 
##  7 Mulch                0 0.15  0.0464   0.15
##  8 Mulch                1 0.17  0.0283   0.15
##  9 Onion                0 0.18  0.0212   0.18
## 10 Onion                1 0.198 0.0303   0.19
## 11 Scallion             0 0.207 0.0473   0.19
## 12 Scallion             1 0.22  0.0361   0.23
## 13 Whitelace            0 0.243 0.0666   0.21
## 14 Whitelace            1 0.233 0.0289   0.25
## # A tibble: 4 × 6
##   term                  df   sumsq  meansq statistic p.value
##   <chr>              <dbl>   <dbl>   <dbl>     <dbl>   <dbl>
## 1 WormPresence           1 0.00135 0.00135     0.215   0.646
## 2 Plant                  6 0.0472  0.00786     1.25    0.302
## 3 WormPresence:Plant     6 0.0213  0.00355     0.565   0.756
## 4 Residuals             40 0.252   0.00629    NA      NA
if (!is.null(results_detailed[["NitN"]])) {
  print(results_detailed[["NitN"]]$SummaryStats)
  print(results_detailed[["NitN"]]$ANOVA)
}
## # A tibble: 14 × 5
##    Plant     WormPresence   Mean      SD Median
##    <chr>            <dbl>  <dbl>   <dbl>  <dbl>
##  1 Chives               0 0.0133 0.00577   0.01
##  2 Chives               1 0.01   0         0.01
##  3 Cilantro             0 0.0333 0.0115    0.04
##  4 Cilantro             1 0.103  0.112     0.06
##  5 Larkspur             0 0.008  0.00837   0.01
##  6 Larkspur             1 0.01   0.0141    0   
##  7 Mulch                0 0.028  0.0295    0.03
##  8 Mulch                1 0.006  0.00894   0   
##  9 Onion                0 0.006  0.00548   0.01
## 10 Onion                1 0.006  0.00548   0.01
## 11 Scallion             0 0.03   0.01      0.03
## 12 Scallion             1 0.0267 0.0153    0.03
## 13 Whitelace            0 0.0133 0.00577   0.01
## 14 Whitelace            1 0.03   0.01      0.03
## # A tibble: 4 × 6
##   term                  df    sumsq   meansq statistic  p.value
##   <chr>              <dbl>    <dbl>    <dbl>     <dbl>    <dbl>
## 1 WormPresence           1 0.000363 0.000363     0.465  0.499  
## 2 Plant                  6 0.0181   0.00302      3.87   0.00391
## 3 WormPresence:Plant     6 0.00866  0.00144      1.85   0.114  
## 4 Residuals             40 0.0313   0.000781    NA     NA
if (!is.null(results_detailed[["AmoN"]])) {
  print(results_detailed[["AmoN"]]$SummaryStats)
  print(results_detailed[["AmoN"]]$ANOVA)
}
## # A tibble: 14 × 5
##    Plant     WormPresence   Mean     SD Median
##    <chr>            <dbl>  <dbl>  <dbl>  <dbl>
##  1 Chives               0 0.07   0.0794   0.04
##  2 Chives               1 0.0767 0.0833   0.05
##  3 Cilantro             0 0.253  0.172    0.18
##  4 Cilantro             1 0.153  0.131    0.11
##  5 Larkspur             0 0.418  0.259    0.27
##  6 Larkspur             1 0.414  0.304    0.45
##  7 Mulch                0 0.374  0.345    0.13
##  8 Mulch                1 0.338  0.383    0.17
##  9 Onion                0 0.408  0.329    0.34
## 10 Onion                1 0.344  0.330    0.2 
## 11 Scallion             0 0.233  0.230    0.24
## 12 Scallion             1 0.167  0.0379   0.15
## 13 Whitelace            0 0.227  0.0907   0.24
## 14 Whitelace            1 0.253  0.163    0.18
## # A tibble: 4 × 6
##   term                  df  sumsq  meansq statistic p.value
##   <chr>              <dbl>  <dbl>   <dbl>     <dbl>   <dbl>
## 1 WormPresence           1 0.0157 0.0157     0.218    0.643
## 2 Plant                  6 0.666  0.111      1.55     0.188
## 3 WormPresence:Plant     6 0.0206 0.00344    0.0479   0.999
## 4 Residuals             40 2.87   0.0718    NA       NA
if (!is.null(results_detailed[["CA"]])) {
  print(results_detailed[["CA"]]$SummaryStats)
  print(results_detailed[["CA"]]$ANOVA)
}
## # A tibble: 14 × 5
##    Plant     WormPresence  Mean    SD Median
##    <chr>            <dbl> <dbl> <dbl>  <dbl>
##  1 Chives               0 18.6   1.88  17.6 
##  2 Chives               1 22.4   3.33  21.4 
##  3 Cilantro             0 16.8   2.34  16.6 
##  4 Cilantro             1 16.6   2.12  17.1 
##  5 Larkspur             0 12.9   1.75  12.5 
##  6 Larkspur             1 19.0   2.08  20   
##  7 Mulch                0  8.73  6.56   5.38
##  8 Mulch                1 11.8   6.75   7.74
##  9 Onion                0 13.1   3.91  12   
## 10 Onion                1 19.1   5.72  21.0 
## 11 Scallion             0 15.5   6.58  13.3 
## 12 Scallion             1 24.6   3.07  24.0 
## 13 Whitelace            0 23.8  10.6   19.6 
## 14 Whitelace            1 22.2   2.45  22.3 
## # A tibble: 4 × 6
##   term                  df sumsq meansq statistic   p.value
##   <chr>              <dbl> <dbl>  <dbl>     <dbl>     <dbl>
## 1 WormPresence           1  219.  219.      9.05   0.00454 
## 2 Plant                  6  816.  136.      5.63   0.000263
## 3 WormPresence:Plant     6  136.   22.7     0.938  0.479   
## 4 Residuals             40  968.   24.2    NA     NA
if (!is.null(results_detailed[["Po"]])) {
  print(results_detailed[["Po"]]$SummaryStats)
  print(results_detailed[["Po"]]$ANOVA)
}
## # A tibble: 14 × 5
##    Plant     WormPresence  Mean    SD Median
##    <chr>            <dbl> <dbl> <dbl>  <dbl>
##  1 Chives               0 11.0  3.52    9.22
##  2 Chives               1  5.38 2.28    5.16
##  3 Cilantro             0 11.8  0.526  11.9 
##  4 Cilantro             1 10.3  3.85   11.7 
##  5 Larkspur             0 15.3  6.03   15.2 
##  6 Larkspur             1 12.3  3.47   11.2 
##  7 Mulch                0  9.70 7.89    8.31
##  8 Mulch                1 12.4  3.77   10.3 
##  9 Onion                0 14.0  8.87   11.0 
## 10 Onion                1 12.1  7.12   12.1 
## 11 Scallion             0 12.0  1.94   12.5 
## 12 Scallion             1  9.58 2.91    8.91
## 13 Whitelace            0 17.8  6.10   15.4 
## 14 Whitelace            1 10.1  1.12   10.0 
## # A tibble: 4 × 6
##   term                  df  sumsq meansq statistic p.value
##   <chr>              <dbl>  <dbl>  <dbl>     <dbl>   <dbl>
## 1 WormPresence           1   72.0   72.0     2.43    0.127
## 2 Plant                  6  178.    29.7     1.01    0.436
## 3 WormPresence:Plant     6  125.    20.8     0.704   0.648
## 4 Residuals             40 1183.    29.6    NA      NA
if (!is.null(results_detailed[["Mag"]])) {
  print(results_detailed[["Mag"]]$SummaryStats)
  print(results_detailed[["Mag"]]$ANOVA)
}
## # A tibble: 14 × 5
##    Plant     WormPresence  Mean    SD Median
##    <chr>            <dbl> <dbl> <dbl>  <dbl>
##  1 Chives               0  2.88 0.275   2.78
##  2 Chives               1  3.29 0.504   3.16
##  3 Cilantro             0  2.63 0.367   2.66
##  4 Cilantro             1  2.80 0.316   2.76
##  5 Larkspur             0  2.09 0.300   2   
##  6 Larkspur             1  2.54 0.450   2.70
##  7 Mulch                0  1.83 0.745   1.65
##  8 Mulch                1  2.23 0.706   1.83
##  9 Onion                0  2.16 0.509   2   
## 10 Onion                1  2.80 0.772   2.91
## 11 Scallion             0  2.49 0.828   2.29
## 12 Scallion             1  3.47 0.504   3.26
## 13 Whitelace            0  3.57 1.40    2.95
## 14 Whitelace            1  3.66 0.449   3.84
## # A tibble: 4 × 6
##   term                  df  sumsq meansq statistic   p.value
##   <chr>              <dbl>  <dbl>  <dbl>     <dbl>     <dbl>
## 1 WormPresence           1  2.86   2.86      7.10   0.0110  
## 2 Plant                  6 12.7    2.11      5.24   0.000460
## 3 WormPresence:Plant     6  0.827  0.138     0.342  0.910   
## 4 Residuals             40 16.1    0.403    NA     NA
if (!is.null(results_detailed[["Pho"]])) {
  print(results_detailed[["Pho"]]$SummaryStats)
  print(results_detailed[["Pho"]]$ANOVA)
}
## # A tibble: 14 × 5
##    Plant     WormPresence  Mean    SD Median
##    <chr>            <dbl> <dbl> <dbl>  <dbl>
##  1 Chives               0 1.45  1.04   1.69 
##  2 Chives               1 1.35  0.998  1.01 
##  3 Cilantro             0 3.79  1.49   3.25 
##  4 Cilantro             1 0.943 0.962  0.906
##  5 Larkspur             0 2.45  0.464  2.5  
##  6 Larkspur             1 1.29  1.32   0.838
##  7 Mulch                0 0.604 0.314  0.7  
##  8 Mulch                1 0.389 0.357  0.285
##  9 Onion                0 2.47  1.98   1.71 
## 10 Onion                1 2.07  2.16   0.985
## 11 Scallion             0 0.935 0.790  1.02 
## 12 Scallion             1 0.693 0.894  0.225
## 13 Whitelace            0 7.77  5.68   5.79 
## 14 Whitelace            1 8.07  4.52   6.50 
## # A tibble: 4 × 6
##   term                  df  sumsq meansq statistic     p.value
##   <chr>              <dbl>  <dbl>  <dbl>     <dbl>       <dbl>
## 1 WormPresence           1   5.70   5.70     1.41   0.242     
## 2 Plant                  6 242.    40.4      9.97   0.00000101
## 3 WormPresence:Plant     6  10.6    1.76     0.435  0.851     
## 4 Residuals             40 162.     4.05    NA     NA
if (!is.null(results_detailed[["AL"]])) {
  print(results_detailed[["AL"]]$SummaryStats)
  print(results_detailed[["AL"]]$ANOVA)
}
## # A tibble: 14 × 5
##    Plant     WormPresence   Mean      SD Median
##    <chr>            <dbl>  <dbl>   <dbl>  <dbl>
##  1 Chives               0 0.094  0.0229   0.099
##  2 Chives               1 0.124  0.0484   0.101
##  3 Cilantro             0 0.108  0.0121   0.109
##  4 Cilantro             1 0.124  0.0331   0.117
##  5 Larkspur             0 0.127  0.0458   0.109
##  6 Larkspur             1 0.114  0.0254   0.104
##  7 Mulch                0 0.139  0.0394   0.144
##  8 Mulch                1 0.121  0.0322   0.103
##  9 Onion                0 0.111  0.0183   0.1  
## 10 Onion                1 0.110  0.0153   0.1  
## 11 Scallion             0 0.121  0.00954  0.116
## 12 Scallion             1 0.131  0.0422   0.136
## 13 Whitelace            0 0.0733 0.0240   0.083
## 14 Whitelace            1 0.0603 0.0142   0.063
## # A tibble: 4 × 6
##   term                  df     sumsq    meansq statistic p.value
##   <chr>              <dbl>     <dbl>     <dbl>     <dbl>   <dbl>
## 1 WormPresence           1 0.0000190 0.0000190    0.0204  0.887 
## 2 Plant                  6 0.0174    0.00290      3.12    0.0133
## 3 WormPresence:Plant     6 0.00342   0.000570     0.615   0.717 
## 4 Residuals             40 0.0371    0.000928    NA      NA
if (!is.null(results_detailed[["BO"]])) {
  print(results_detailed[["BO"]]$SummaryStats)
  print(results_detailed[["BO"]]$ANOVA)
}
## # A tibble: 14 × 5
##    Plant     WormPresence   Mean      SD Median
##    <chr>            <dbl>  <dbl>   <dbl>  <dbl>
##  1 Chives               0 0.0233 0.00451  0.023
##  2 Chives               1 0.0203 0.00208  0.021
##  3 Cilantro             0 0.0457 0.0102   0.05 
##  4 Cilantro             1 0.0293 0.00611  0.028
##  5 Larkspur             0 0.0322 0.0160   0.028
##  6 Larkspur             1 0.0382 0.0208   0.032
##  7 Mulch                0 0.0578 0.00867  0.058
##  8 Mulch                1 0.0472 0.0226   0.038
##  9 Onion                0 0.0474 0.0330   0.029
## 10 Onion                1 0.038  0.0239   0.03 
## 11 Scallion             0 0.036  0.00624  0.038
## 12 Scallion             1 0.0407 0.0163   0.044
## 13 Whitelace            0 0.0317 0.0124   0.025
## 14 Whitelace            1 0.036  0.01     0.036
## # A tibble: 4 × 6
##   term                  df    sumsq   meansq statistic p.value
##   <chr>              <dbl>    <dbl>    <dbl>     <dbl>   <dbl>
## 1 WormPresence           1 0.000189 0.000189     0.573  0.453 
## 2 Plant                  6 0.00405  0.000674     2.05   0.0818
## 3 WormPresence:Plant     6 0.000877 0.000146     0.444  0.845 
## 4 Residuals             40 0.0132   0.000330    NA     NA
if (!is.null(results_detailed[["CO"]])) {
  print(results_detailed[["CO"]]$SummaryStats)
  print(results_detailed[["CO"]]$ANOVA)
}
## # A tibble: 14 × 5
##    Plant     WormPresence    Mean      SD Median
##    <chr>            <dbl>   <dbl>   <dbl>  <dbl>
##  1 Chives               0 0.00167 0.00289 0     
##  2 Chives               1 0.005   0       0.005 
##  3 Cilantro             0 0.012   0.00173 0.011 
##  4 Cilantro             1 0.00767 0.00208 0.007 
##  5 Larkspur             0 0.00756 0.00293 0.008 
##  6 Larkspur             1 0.00456 0.00269 0.0049
##  7 Mulch                0 0.00536 0.00116 0.0049
##  8 Mulch                1 0.00298 0.00281 0.004 
##  9 Onion                0 0.00498 0.00324 0.005 
## 10 Onion                1 0.00678 0.00134 0.007 
## 11 Scallion             0 0       0       0     
## 12 Scallion             1 0.00433 0.00404 0.005 
## 13 Whitelace            0 0.00733 0.00153 0.007 
## 14 Whitelace            1 0.00967 0.00321 0.011 
## # A tibble: 4 × 6
##   term                  df        sumsq       meansq statistic    p.value
##   <chr>              <dbl>        <dbl>        <dbl>     <dbl>      <dbl>
## 1 WormPresence           1 0.0000000150 0.0000000150   0.00251  0.960    
## 2 Plant                  6 0.000283     0.0000471      7.90     0.0000120
## 3 WormPresence:Plant     6 0.000126     0.0000210      3.52     0.00691  
## 4 Residuals             40 0.000239     0.00000597    NA       NA
if (!is.null(results_detailed[["IR"]])) {
  print(results_detailed[["IR"]]$SummaryStats)
  print(results_detailed[["IR"]]$ANOVA)
}
## # A tibble: 14 × 5
##    Plant     WormPresence   Mean      SD Median
##    <chr>            <dbl>  <dbl>   <dbl>  <dbl>
##  1 Chives               0 0.032  0.00173  0.033
##  2 Chives               1 0.0277 0.00569  0.026
##  3 Cilantro             0 0.0263 0.00404  0.024
##  4 Cilantro             1 0.0247 0.00153  0.025
##  5 Larkspur             0 0.0412 0.00729  0.042
##  6 Larkspur             1 0.0382 0.0125   0.04 
##  7 Mulch                0 0.0474 0.0110   0.05 
##  8 Mulch                1 0.0316 0.0119   0.034
##  9 Onion                0 0.0336 0.00879  0.03 
## 10 Onion                1 0.0294 0.0105   0.029
## 11 Scallion             0 0.0337 0.00321  0.035
## 12 Scallion             1 0.0327 0.00115  0.032
## 13 Whitelace            0 0.017  0.00436  0.015
## 14 Whitelace            1 0.018  0.00173  0.019
## # A tibble: 4 × 6
##   term                  df    sumsq    meansq statistic    p.value
##   <chr>              <dbl>    <dbl>     <dbl>     <dbl>      <dbl>
## 1 WormPresence           1 0.000328 0.000328      4.65   0.0371   
## 2 Plant                  6 0.00271  0.000451      6.40   0.0000868
## 3 WormPresence:Plant     6 0.000398 0.0000664     0.943  0.475    
## 4 Residuals             40 0.00282  0.0000704    NA     NA
if (!is.null(results_detailed[["Man"]])) {
  print(results_detailed[["Man"]]$SummaryStats)
  print(results_detailed[["Man"]]$ANOVA)
}
## # A tibble: 14 × 5
##    Plant     WormPresence   Mean       SD Median
##    <chr>            <dbl>  <dbl>    <dbl>  <dbl>
##  1 Chives               0 0      0             0
##  2 Chives               1 0      0             0
##  3 Cilantro             0 0      0             0
##  4 Cilantro             1 0      0             0
##  5 Larkspur             0 0.0004 0.000548      0
##  6 Larkspur             1 0.0004 0.000548      0
##  7 Mulch                0 0.004  0.00548       0
##  8 Mulch                1 0.0004 0.000548      0
##  9 Onion                0 0.0022 0.00438       0
## 10 Onion                1 0.0004 0.000548      0
## 11 Scallion             0 0      0             0
## 12 Scallion             1 0      0             0
## 13 Whitelace            0 0      0             0
## 14 Whitelace            1 0      0             0
## # A tibble: 4 × 6
##   term                  df     sumsq     meansq statistic p.value
##   <chr>              <dbl>     <dbl>      <dbl>     <dbl>   <dbl>
## 1 WormPresence           1 0.0000135 0.0000135      2.68    0.110
## 2 Plant                  6 0.0000387 0.00000646     1.28    0.288
## 3 WormPresence:Plant     6 0.000027  0.0000045      0.893   0.509
## 4 Residuals             40 0.000202  0.00000504    NA      NA
if (!is.null(results_detailed[["MO"]])) {
  print(results_detailed[["MO"]]$SummaryStats)
  print(results_detailed[["MO"]]$ANOVA)
}
## # A tibble: 14 × 5
##    Plant     WormPresence    Mean      SD Median
##    <chr>            <dbl>   <dbl>   <dbl>  <dbl>
##  1 Chives               0 0.0543  0.0544  0.034 
##  2 Chives               1 0.00467 0.00451 0.005 
##  3 Cilantro             0 0       0       0     
##  4 Cilantro             1 0.00733 0.00153 0.007 
##  5 Larkspur             0 0.0302  0.0371  0.01  
##  6 Larkspur             1 0.0086  0.00313 0.008 
##  7 Mulch                0 0.00196 0.00268 0     
##  8 Mulch                1 0.00196 0.00268 0     
##  9 Onion                0 0.00296 0.00270 0.0049
## 10 Onion                1 0.00298 0.00272 0.0049
## 11 Scallion             0 0.00167 0.00289 0     
## 12 Scallion             1 0       0       0     
## 13 Whitelace            0 0       0       0     
## 14 Whitelace            1 0.0127  0.00651 0.013 
## # A tibble: 4 × 6
##   term                  df    sumsq   meansq statistic p.value
##   <chr>              <dbl>    <dbl>    <dbl>     <dbl>   <dbl>
## 1 WormPresence           1 0.000755 0.000755      2.57  0.117 
## 2 Plant                  6 0.00508  0.000846      2.88  0.0198
## 3 WormPresence:Plant     6 0.00444  0.000740      2.52  0.0366
## 4 Residuals             40 0.0117   0.000293     NA    NA
if (!is.null(results_detailed[["SO"]])) {
  print(results_detailed[["SO"]]$SummaryStats)
  print(results_detailed[["SO"]]$ANOVA)
}
## # A tibble: 14 × 5
##    Plant     WormPresence  Mean    SD Median
##    <chr>            <dbl> <dbl> <dbl>  <dbl>
##  1 Chives               0  23.3 4.07    21.6
##  2 Chives               1  21.2 2.20    20.7
##  3 Cilantro             0  20.1 1.43    19.3
##  4 Cilantro             1  24.5 8.40    19.8
##  5 Larkspur             0  20.4 2.81    19  
##  6 Larkspur             1  22.2 2.41    21.1
##  7 Mulch                0  18.1 3.15    18.9
##  8 Mulch                1  19.5 2.65    18.7
##  9 Onion                0  19.7 2.40    21  
## 10 Onion                1  21.1 3.31    21  
## 11 Scallion             0  23.6 4.71    22.8
## 12 Scallion             1  22.4 2.58    22.9
## 13 Whitelace            0  20.7 0.518   20.8
## 14 Whitelace            1  24.4 3.38    24.2
## # A tibble: 4 × 6
##   term                  df sumsq meansq statistic p.value
##   <chr>              <dbl> <dbl>  <dbl>     <dbl>   <dbl>
## 1 WormPresence           1  25.7  25.7      2.24    0.142
## 2 Plant                  6 108.   18.0      1.57    0.180
## 3 WormPresence:Plant     6  51.4   8.56     0.748   0.615
## 4 Residuals             40 458.   11.5     NA      NA
if (!is.null(results_detailed[["SU"]])) {
  print(results_detailed[["SU"]]$SummaryStats)
  print(results_detailed[["SU"]]$ANOVA)
}
## # A tibble: 14 × 5
##    Plant     WormPresence  Mean    SD Median
##    <chr>            <dbl> <dbl> <dbl>  <dbl>
##  1 Chives               0  8.45 3.71    9.10
##  2 Chives               1  7.67 4.49    6.65
##  3 Cilantro             0  5.19 1.31    5.51
##  4 Cilantro             1  2.83 0.273   2.97
##  5 Larkspur             0  6.54 1.99    5.96
##  6 Larkspur             1  5.92 1.48    5.00
##  7 Mulch                0  3.19 0.960   3   
##  8 Mulch                1  2.71 1.41    2.28
##  9 Onion                0  6.05 1.42    6.03
## 10 Onion                1  5.79 2.69    5.28
## 11 Scallion             0  7.28 4.12    5.34
## 12 Scallion             1  5.76 1.33    5.24
## 13 Whitelace            0  8.22 4.23    6.10
## 14 Whitelace            1  6.25 1.83    7.15
## # A tibble: 4 × 6
##   term                  df  sumsq meansq statistic  p.value
##   <chr>              <dbl>  <dbl>  <dbl>     <dbl>    <dbl>
## 1 WormPresence           1  13.2   13.2      2.35   0.133  
## 2 Plant                  6 148.    24.6      4.38   0.00171
## 3 WormPresence:Plant     6   7.06   1.18     0.209  0.972  
## 4 Residuals             40 225.     5.62    NA     NA
if (!is.null(results_detailed[["Zi"]])) {
  print(results_detailed[["Zi"]]$SummaryStats)
  print(results_detailed[["Zi"]]$ANOVA)
}
## # A tibble: 14 × 5
##    Plant     WormPresence   Mean      SD Median
##    <chr>            <dbl>  <dbl>   <dbl>  <dbl>
##  1 Chives               0 0.0377 0.00551  0.038
##  2 Chives               1 0.027  0.00265  0.028
##  3 Cilantro             0 0.0573 0.0129   0.061
##  4 Cilantro             1 0.0483 0.0170   0.047
##  5 Larkspur             0 0.0564 0.0303   0.045
##  6 Larkspur             1 0.0628 0.0313   0.065
##  7 Mulch                0 0.0652 0.0242   0.079
##  8 Mulch                1 0.048  0.0134   0.043
##  9 Onion                0 0.0946 0.0926   0.046
## 10 Onion                1 0.0664 0.0430   0.06 
## 11 Scallion             0 0.0353 0.00850  0.035
## 12 Scallion             1 0.054  0.0154   0.05 
## 13 Whitelace            0 0.0677 0.0516   0.049
## 14 Whitelace            1 0.0793 0.0214   0.088
## # A tibble: 4 × 6
##   term                  df    sumsq   meansq statistic p.value
##   <chr>              <dbl>    <dbl>    <dbl>     <dbl>   <dbl>
## 1 WormPresence           1 0.000492 0.000492     0.327   0.571
## 2 Plant                  6 0.0117   0.00195      1.29    0.282
## 3 WormPresence:Plant     6 0.00336  0.000560     0.372   0.892
## 4 Residuals             40 0.0602   0.00150     NA      NA
# ------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------

# Function to analyze and plot data
analyze_and_plot_elements <- function(data) {
  # Loop over each element
  unique_elements <- unique(data$element)
  
  plots <- list()  # List to store ggplot objects
  
  for (elem in unique_elements) {
    # Filter data for the specific element
    elem_data <- data %>% filter(element == elem)
    
    # Calculate summary statistics
    summary_stats <- elem_data %>% 
      group_by(Plant, WormPresence) %>% 
      summarise(
        Mean = mean(amount, na.rm = TRUE),
        SD = sd(amount, na.rm = TRUE),
        .groups = 'drop'
      ) %>%
      ungroup() %>%
      mutate(WormPresence = factor(WormPresence, levels = c(0, 1), labels = c("No Worms", "Worms")))

    # Generate plot
    p <- ggplot(summary_stats, aes(x = Plant, y = Mean, fill = WormPresence)) +
      geom_bar(stat = "identity", position = position_dodge(width = 0.8), width = 0.7) +
      geom_errorbar(aes(ymin = Mean - SD, ymax = Mean + SD), width = 0.25, position = position_dodge(width = 0.8)) +
      facet_wrap(~WormPresence) +
      labs(title = paste("Element:", elem),
           y = "Mean Amount with SD",
           x = "Plant Type") +
      theme_minimal() +
      theme(axis.text.x = element_text(angle = 45, hjust = 1))

    # Store plot in list
    plots[[elem]] <- p
  }
  
  return(plots)
}

# Usage
plots <- analyze_and_plot_elements(long_dat)

# To display plots for a specific element, e.g., "Ph"
if (!is.null(plots[["Ph"]])) {
  print(plots[["Ph"]])
}

if (!is.null(plots[["SS"]])) {
  print(plots[["SS"]])
}

if (!is.null(plots[["NitN"]])) {
  print(plots[["NitN"]])
}

if (!is.null(plots[["AmoN"]])) {
  print(plots[["AmoN"]])
}

if (!is.null(plots[["CA"]])) {
  print(plots[["CA"]])
}

if (!is.null(plots[["Po"]])) {
  print(plots[["Po"]])
}

if (!is.null(plots[["Mag"]])) {
  print(plots[["Mag"]])
}

if (!is.null(plots[["Pho"]])) {
  print(plots[["Pho"]])
}

if (!is.null(plots[["AL"]])) {
  print(plots[["AL"]])
}

if (!is.null(plots[["BO"]])) {
  print(plots[["BO"]])
}

if (!is.null(plots[["CO"]])) {
  print(plots[["CO"]])
}

if (!is.null(plots[["IR"]])) {
  print(plots[["IR"]])
}

if (!is.null(plots[["Man"]])) {
  print(plots[["MAn"]])
}
## NULL
if (!is.null(plots[["MO"]])) {
  print(plots[["MO"]])
}

if (!is.null(plots[["SO"]])) {
  print(plots[["SO"]])
}

if (!is.null(plots[["SU"]])) {
  print(plots[["SU"]])
}

if (!is.null(plots[["Zi"]])) {
  print(plots[["Zi"]])
}

# ------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------
# Function to plot the summary statistics and ANOVA results
plot_element_results <- function(results) {
  plots <- list()  # List to store ggplot objects

  # Loop over each element in the results list
  for (elem in names(results)) {
    summary_stats <- results[[elem]]$SummaryStats
    aov_results <- results[[elem]]$ANOVA

    # Ensure WormPresence is a factor with proper labels
    summary_stats$WormPresence <- factor(summary_stats$WormPresence, levels = c(0, 1), labels = c("No Worm", "Worm"))

    # Create the plot for summary statistics
    p <- ggplot(summary_stats, aes(x = Plant, y = Mean, fill = WormPresence)) +
      geom_bar(stat = "identity", position = position_dodge(), width = 0.7, color="black",size=1) +
      geom_errorbar(aes(ymin = Mean - SD, ymax = Mean + SD), width = 0.25, position = position_dodge(0.7)) +
geom_point(position = position_jitter(width = 0.2, height = 0), color = "black", size = 1)+
      facet_wrap(~WormPresence) +
      labs(title = paste("Element:", elem, "\nANOVA p-value:", min(aov_results$p.value, na.rm = TRUE)),
           x = "Plant Type",
           y = "Mean Amount with SD",
           fill = "Worm Presence") +
      scale_fill_manual(values = c("No Worm" = "thistle", "Worm" = "goldenrod"),  # Specifying actual colors for "No Worm" and "Worm"
                        labels = c("No Worm", "Worm")) +
      theme_minimal() +
      theme(axis.text.x = element_text(angle = 45, hjust = 1))

    # Store the plot
    plots[[elem]] <- p
  }

  return(plots)
}

# Rerun the plotting function with the corrected settings
plots <- plot_element_results(results_detailed)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# Display plot for a specific element, e.g., "Ph"
if (!is.null(plots[["Ph"]])) {
  print(plots[["Ph"]])
}

if (!is.null(plots[["SS"]])) {
  print(plots[["SS"]])
}

if (!is.null(plots[["NitN"]])) {
  print(plots[["NitN"]])
}

if (!is.null(plots[["AmoN"]])) {
  print(plots[["AmoN"]])
}

if (!is.null(plots[["CA"]])) {
  print(plots[["CA"]])
}

if (!is.null(plots[["Po"]])) {
  print(plots[["Po"]])
}

if (!is.null(plots[["Mag"]])) {
  print(plots[["MAg"]])
}
## NULL
if (!is.null(plots[["Pho"]])) {
  print(plots[["Pho"]])
}

if (!is.null(plots[["AL"]])) {
  print(plots[["AL"]])
}

if (!is.null(plots[["BO"]])) {
  print(plots[["BO"]])
}

if (!is.null(plots[["CO"]])) {
  print(plots[["CO"]])
}

if (!is.null(plots[["IR"]])) {
  print(plots[["IR"]])
}

if (!is.null(plots[["Man"]])) {
  print(plots[["Man"]])
}

if (!is.null(plots[["MO"]])) {
  print(plots[["MO"]])
}

if (!is.null(plots[["SO"]])) {
  print(plots[["SO"]])
}

if (!is.null(plots[["SO"]])) {
  print(plots[["SO"]])
}

if (!is.null(plots[["SU"]])) {
  print(plots[["SU"]])
}

if (!is.null(plots[["Zi"]])) {
  print(plots[["Zi"]])
}

# Display the plot for 'Zi', if it exists
if (!is.null(plots[["Zi"]])) {
  print(plots[["Zi"]])
} else {
  print("No plot available for 'Zi'. Check if 'Zi' is a valid element in your results.")
}